﻿using Sc.Domain;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sc.Persistence
{
    public class DriverScoreAppealsDAO
    {
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(DriverScoreAppeals model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into DriverScoreAppeals(");
            strSql.Append("DriverScoreId,InstanceState,ApproalUsres,NodeCount,UserId,AppealReson)");
            strSql.Append(" values (");
            strSql.Append("@DriverScoreId,@InstanceState,@ApproalUsres,@NodeCount,@UserId,@AppealReson)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@DriverScoreId", SqlDbType.Int,4),
					new SqlParameter("@InstanceState", SqlDbType.Int,4),
					new SqlParameter("@ApproalUsres", SqlDbType.VarChar,100),
					new SqlParameter("@NodeCount", SqlDbType.Int,4),
					new SqlParameter("@UserId", SqlDbType.Int,4),
					new SqlParameter("@AppealReson", SqlDbType.NVarChar,200)};
            parameters[0].Value = model.DriverScoreId;
            parameters[1].Value = model.InstanceState;
            parameters[2].Value = model.ApproalUsres;
            parameters[3].Value = model.NodeCount;
            parameters[4].Value = model.UserId;
            parameters[5].Value = model.AppealReson;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DriverScoreAppeals GetModel(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from Vw_DriverScoreAppeals ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            DriverScoreAppeals model = new DriverScoreAppeals();
            DataTable ds = DbHelperSQL.Query(CommandType.Text,strSql.ToString(), parameters);
            if (ds.Rows.Count > 0)
            {
                return DataRowToModel(ds.Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DriverScoreAppeals DataRowToModel(DataRow row)
        {
            DriverScoreAppeals model = new DriverScoreAppeals();
            if (row != null)
            {
                if (row["Id"] != null && row["Id"].ToString() != "")
                {
                    model.Id = int.Parse(row["Id"].ToString());
                }
                if (row["DriverScoreId"] != null && row["DriverScoreId"].ToString() != "")
                {
                    model.DriverScoreId = int.Parse(row["DriverScoreId"].ToString());
                }
                if (row["InstanceState"] != null && row["InstanceState"].ToString() != "")
                {
                    model.InstanceState = int.Parse(row["InstanceState"].ToString());
                }
                if (row["ApproalUsres"] != null)
                {
                    model.ApproalUsres = row["ApproalUsres"].ToString();
                }
                if (row["NodeCount"] != null && row["NodeCount"].ToString() != "")
                {
                    model.NodeCount = int.Parse(row["NodeCount"].ToString());
                }
                if (row["UserId"] != null && row["UserId"].ToString() != "")
                {
                    model.UserId = int.Parse(row["UserId"].ToString());
                }
                if (row["AppealReson"] != null)
                {
                    model.AppealReson = row["AppealReson"].ToString();
                }
                if (row["CreateTime"] != null && row["CreateTime"].ToString() != "")
                {
                    model.CreateTime = DateTime.Parse(row["CreateTime"].ToString());
                }
                if (row["Score"] != null && row["Score"].ToString() != "")
                {
                    model.Score = Convert.ToDecimal(row["Score"].ToString());
                }
                if (row["Type"] != null && row["Type"].ToString() != "")
                {
                    model.Type = Convert.ToInt32(row["Type"].ToString());
                }
                if (row["BussId"] != null && row["BussId"].ToString() != "")
                {
                    model.BussId = Convert.ToInt32(row["BussId"].ToString());
                }
            }
            return model;
        }
        /// <summary>
        /// 更新评分申诉
        /// </summary>
        /// <param name="ent">业务数据</param>
        /// <returns></returns>
        public int Update(DriverScoreAppeals ent)
        {
            string sql = "UPDATE DriverScoreAppeals SET InstanceState=@InstanceState,ApproalUsres=@ApproalUsres,NodeCount=@NodeCount WHERE Id=@Id";
            SqlParameter[] parameters = new SqlParameter[]{
                new SqlParameter("@InstanceState",SqlDbType.Int),
                new SqlParameter("@ApproalUsres",SqlDbType.NVarChar),
                new SqlParameter("@NodeCount",SqlDbType.Int),
                new SqlParameter("@Id",SqlDbType.Int)
            };
            parameters[0].Value = ent.InstanceState;
            parameters[1].Value = ent.ApproalUsres;
            parameters[2].Value = ent.NodeCount;
            parameters[3].Value = ent.Id;
            return DbHelperSQL.ExecuteSql(sql, parameters);
        }
    }
}
